package com.ton.config.queue;

import com.hai.admin.table.vo.Result;
import com.ton.utils.quartz.QueueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.*;

@Component
@Slf4j
public class QueueConfig {

    private BlockingQueue<FutureTask<Object>> blockingQueue;


    /**
     * 用于并发收款 监听的队列
     * @return
     */
    @Bean
    BlockingQueue<FutureTask<Object>> blockingQueue() {
        // 创建一个队列
        BlockingQueue<FutureTask<Object>> taskQueue = new LinkedBlockingQueue<>();

        // 创建一个线程池
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.scheduleAtFixedRate(() -> {
            // 从任务队列中取出一个任务并执行
            FutureTask<Object> task = taskQueue.poll();
            if (task != null) {
                Future<?> future = executor.submit(task);// 提交任务到线程池执行
                System.out.println("线程池执行...");
            }
        }, 0, 3, TimeUnit.SECONDS);
        return taskQueue;
    }
}

